package aos.client.gui;

import java.awt.Color;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JSlider;

import aos.client.robot.RemoteRobotControllerProxy;
import aos.server.robot.RobotControllerProxy;

import thinlet.Button;
import thinlet.Menu;
import thinlet.MenuBar;
import thinlet.Pane;
import thinlet.RootPane;
import thinlet.ScrollPanel;
import thinlet.Slider;
import thinlet.Text;
import thinlet.Widget;
import thinlet.event.Listener;
import thinlet.layout.Constraint;
import thinlet.layout.InlineLayout;
import thinlet.layout.Layout;
import thinlet.layout.Metrics;
import thinlet.layout.RowLayout;
import thinlet.layout.TableLayout;


public class ControlPane extends Pane {

	PadSliderCoordinator coordinator;
	RobotControllerProxy robotControllerProxy;
	
	public ControlPane(RobotControllerProxy robotControllerProxy) {
		
		setLayout(new RowLayout());
		Slider horizontalSlider = new Slider();
		VerticalSlider verticalSlider = new VerticalSlider();
		Pad pad = new Pad();
		pad.setBounds(0, 0, 600, 150);
		Pane border = new Pane();
		border.setLayout(new TableLayout(11, 0, 0, 0, 0, 4, 4));
		
		coordinator = new PadSliderCoordinator(horizontalSlider, verticalSlider, pad);
		coordinator.addChangeListener(new Listener(this, "padControllerChanged"));
		this.robotControllerProxy = robotControllerProxy;
		
		
		
		border.add(new Block(" "));
		border.add(new Block(" "));
		border.add(new Block("4"));
		border.add(new Block("3"));
		border.add(new Block("2"));
		border.add(new Block("1"));
		border.add(new Block("0"));
		border.add(new Block("1"));
		border.add(new Block("2"));
		border.add(new Block("3"));
		border.add(new Block("4"));
		
		border.add(new Block(" "));
		border.add(new Block(" "));
		border.add(horizontalSlider.setConstraint(new Constraint(9, 1)));
		
		border.add(new Block("2"));
		border.add(verticalSlider.setConstraint(new Constraint(1, 5)));
		border.add(pad.setConstraint(new Constraint(9, 5)));
		
		border.add(new Block("1"));
		border.add(new Block("0"));
		border.add(new Block("1"));
		border.add(new Block("2"));
		
		add(border);
		
	}

	public void padControllerChanged() {
		robotControllerProxy.setPosition(coordinator.pad.getValueX(), coordinator.pad.getValueY());
	}
	
	

	private static class Block extends Widget {

		private static final Layout layout = new InlineLayout(2, 5, 2, 5, 4);
		{ setLayout(layout); }

		private Block(String text) {
			add(new Text(text));
		}

		protected void paint(Graphics2D g) {
			g.setColor(Color.lightGray);
			g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
			g.setColor(new Color(0xe0e0e0));
			g.fillRect(1, 1, getWidth() - 2, getHeight() - 2);
			g.setColor(Color.black);
		}

		protected boolean isFocusable() {
			return false;
		}

		protected Widget getWidgetAt(int mx, int my) {
			return null;
		}
	}
}
